package com.itkjb.leisurely.resource.sys.controller;


import com.itkjb.leisurely.core.base.BaseConstants;
import com.itkjb.leisurely.core.base.BaseResult;
import com.itkjb.leisurely.core.base.controller.BaseController;
import com.itkjb.leisurely.resource.sys.model.SysRoleMenuModel;
import com.itkjb.leisurely.resource.sys.model.vo.SysRoleMenuVO;
import com.itkjb.leisurely.resource.sys.service.ISysRoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
/**
 * <p>
 * 角色绑定菜单 接口层
 * </p>
 *
 * @author itkjb.com
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/sys/sys-role-menu")
@Api(value="角色绑定菜单",tags={"角色绑定菜单相关接口"})
public class SysRoleMenuController extends BaseController<SysRoleMenuModel,ISysRoleMenuService> {



    /**
     * 根据roleId获取已绑定的菜单Id
     * @param id roldId
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/roleId/{id}")
    @ApiOperation(value="根据roleId获取已绑定的菜单Id",notes="")
    @PreAuthorize("hasAnyAuthority('SysRole:SysRoleMenu','SysRole:SysRoleMenu:query','SysRoleMenu','SysRoleMenu:query') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<List<SysRoleMenuModel>> ListByRoleId(@PathVariable("id") Serializable id,HttpServletRequest request, HttpServletResponse response){
        BaseResult<List<SysRoleMenuModel>> res = new BaseResult<>();
        List<SysRoleMenuModel> list = this.baseService.list(id, true);
        res.toSuccess().setData(list);
        return res;
    }

    /**
     * 根据roleId获取已绑定的菜单Id
     * @param id roldId
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/menuId/{id}")
    @ApiOperation(value="根据菜单id获取已绑定的角色Id",notes="")
    @PreAuthorize("hasAnyAuthority('SysRole:SysRoleMenu','SysRole:SysRoleMenu:query','SysRoleMenu','SysRoleMenu:query') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<List<SysRoleMenuModel>> ListByMenuId(@PathVariable("id") Serializable id,HttpServletRequest request, HttpServletResponse response){
        BaseResult<List<SysRoleMenuModel>> res = new BaseResult<>();
        List<SysRoleMenuModel> list = this.baseService.list(id, false);
        res.setData(list).toSuccess();
        return res;
    }

    /**
     * 新增角色绑定菜单
     * @param param
     * @param request
     * @param response
     * @return
     */
    @PostMapping
    @ApiOperation(value="新增角色绑定菜单",notes="根据角色Id绑定多个菜单，注意这里的绑定的菜单必须是全量即所有指定要绑定的菜单，因为这个接口会覆盖掉原来该角色绑定的所有菜单")
    @PreAuthorize("hasAnyAuthority('SysRole:SysRoleMenu:add','SysRoleMenu:add') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysRoleMenuVO> add(@Valid @RequestBody SysRoleMenuVO param, HttpServletRequest request, HttpServletResponse response){
        BaseResult<SysRoleMenuVO> res = new BaseResult<>();
        if(StringUtils.isBlank(param.getRoleId())){
            return res.setMsg("请选择要分配的角色");
        }
        this.baseService.saveBindByRoleId(param);
        // this.baseService.
        res.toSuccess();

        return res;
    }

    /**
     * 获取角色绑定菜单详情
     * @param id
     * @param response
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value="获取角色绑定菜单详情",notes="")
    @PreAuthorize("hasAnyAuthority('SysRole:SysRoleMenu:info','SysRoleMenu:info') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    @ApiParam(name = "id",type = "Serializable",required = true)
    public BaseResult<SysRoleMenuModel> info(@PathVariable("id") Serializable id, HttpServletRequest request, HttpServletResponse response){
        BaseResult<SysRoleMenuModel> res = new BaseResult<>();
        res = super.info(id);
        return res;
    }



    /**
     * 删除角色绑定菜单
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @DeleteMapping
    @ApiOperation(value="删除角色绑定菜单",notes="注意参数是数字，就算单个，也要以数字的形式传递")
    @PreAuthorize("hasAnyAuthority('SysRole:SysRoleMenu:del','SysRoleMenu:del') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysRoleMenuModel> delete(@RequestBody String[] ids, HttpServletRequest request, HttpServletResponse response){
        BaseResult<SysRoleMenuModel> res = new BaseResult<>();
        res = super.delete(ids);
        return res;
    }

}

